<template>
	<view class="container">
		<view class="center_bgi">
			<u-navbar title="个人中心" @rightClick="rightClick" :autoBack="true" bgColor="transparent">
			</u-navbar>
			<view class="" v-if="user.user_id">
				<view class="center_head">
					<image :src="user.head_pic" mode="" class="image_1" @click="getUserProfile"></image>
					<view class="center_head_flex">
						<view class="center_head_top">
							<view class="center_head_flex_1">
								<text class="nickname">{{user.nickname}}</text>
								<view class="center_head_top_bg">
									{{user.level_name}}
								</view>
							</view>
							<text>{{user.mobile}}</text>
						</view>
					</view>
					<view class="right-text" @click="personalto">我的信息</view>
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/install.png" v-if="false"
						class="image_2" @click="personalto"></image>
				</view>
			</view>
			<view class="" v-else>
				<view class="center_head">
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/3.png" @click="loginto"
						mode="" class="image_1"></image>
					<view class="center_head_flex">
						<view class="center_head_top" @click="loginto">
							<view class="center_head_flex_1">
								<text class="nickname">点击登录</text>
							</view>
							<text></text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="inner">
			<!-- <block v-if="user.user_id">
				<view class="center_balance">
					<view class="center_balance_flex" @click="balanceto(4)">
						<text>{{user.reserve_money}}</text>
						<text>储备金</text>
					</view>
					<view class="center_balance_flex" @click="balanceto(3)">
						<text>{{user. mcbt}}</text>
						<text>补贴</text>
					</view>
					<view class="center_balance_flex" @click="balanceto(2)">
						<text>{{user.jifen}}</text>
						<text>积分</text>
					</view>
					<view class="center_balance_flex" @click="balanceto(1)">
						<text>{{user.user_money}}</text>
						<text>粮票</text>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="center_balance">
					<view class="center_balance_flex">
						<text>0.00</text>
						<text>储备金</text>
					</view>
					<view class="center_balance_flex">
						<text>0.00</text>
						<text>补贴</text>
					</view>
					<view class="center_balance_flex">
						<text>0.00</text>
						<text>积分</text>
					</view>
					<view class="center_balance_flex">
						<text>0.00</text>
						<text>粮票</text>
					</view>
				</view>
			</block> -->
			<view class="center_Myorder frist">
				<view class="center_Myorder_top bor">
					<text class="center_Myorder_top_text1">我的订单</text>
					<view class="all-order" @click="orderto">
						<text class="center_Myorder_top_text2">查看全部订单</text>
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/drop-down2.png"
							mode="widthFix"></image>
					</view>
				</view>
				<view class="center_Myorder_bottom">
					<view class="center_Myorder_bottom_flex" v-for="(item,index) in orderlist" :key="index"
						@tap='jump(`/pages/order/order-list`,index+1)'>
						<image :src="item.img" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">{{item.text}}</text>
					</view>
				</view>
			</view>
			<!-- <view class="center_Myorder">
				<view class="center_Myorder_top">
					<text class="center_Myorder_top_text1">常用功能</text>
				</view>
				<view class="center_Myorder_bottom2">
					<view class="center_Myorder_bottom_flex2" @click="toMyMoney">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/txzr.png" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">我的资金</text>
					</view>
					<view class="center_Myorder_bottom_flex2" v-for="(item,index) in functionlist" :key="index"
						@click="functionto(item.page)">
						<image :src="item.img" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">{{item.text}}</text>
					</view>
				</view>
			</view> -->
			<view class="center_Myorder">
				<view class="center_Myorder_top">
					<text class="center_Myorder_top_text1">我的资产</text>
				</view>
				<view class="center_Myorder_bottom2">
					<view class="center_Myorder_bottom_flex2" v-for="(item,index) in myMoney" :key="index"
						@click="functionto(item.page)">
						<image :src="item.img" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">{{item.text}}</text>
					</view>
				</view>
			</view>
			<view class="center_Myorder">
				<view class="center_Myorder_top">
					<text class="center_Myorder_top_text1">常用管理</text>
				</view>
				<view class="center_Myorder_bottom2">
					<view class="center_Myorder_bottom_flex2" v-for="(item,index) in regularManager" :key="index"
						@click="functionto(item.page)">
						<image :src="item.img" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">{{item.text}}</text>
					</view>
				</view>
			</view>
			<view class="center_Myorder">
				<view class="center_Myorder_top">
					<text class="center_Myorder_top_text1">商家服务</text>
				</view>
				<view class="center_Myorder_bottom2">
					<view class="center_Myorder_bottom_flex2" v-for="(item,index) in merchantService" :key="index"
						@click="functionto(item.page)">
						<image :src="item.img" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">{{item.text}}</text>
					</view>
				</view>
			</view>
			<view class="center_Myorder">
				<view class="center_Myorder_top">
					<text class="center_Myorder_top_text1">平台服务</text>
				</view>
				<view class="center_Myorder_bottom2">
					<view class="center_Myorder_bottom_flex2" v-for="(item,index) in platFormService" :key="index"
						@click="functionto(item.page)">
						<image :src="item.img" mode="" class="center_Myorder_bottom_img"></image>
						<text class="center_Myorder_bottom_text">{{item.text}}</text>
					</view>
				</view>
			</view>
		</view>

		<view style="height: 110rpx;"></view>
		<u-popup :safeAreaInsetBottom="false" :safeAreaInsetTop="false" mode="center" :show="show" round="5"
			:overlay="true" :customStyle="customStyle1" @close="close" @open="open">
			<view class="u-popup-slot">
				<text class="u-popup-slot_text">确认退出登录？</text>
				<view class="u-popup-slot_flex">
					<button @click="show = !show" class="cancel">取消</button>
					<button @click="Logout()" class="confirm">确认</button>
				</view>
			</view>
		</u-popup>
		<u-popup :closeOnClickOverlay="false" :show="loginShow" @close="loginShow=false" mode="center"
			bgColor="transparent" :overlayOpacity="0.6">
			<view class="weixin-login">
				<view class="weixin-login-info">
					<text class="text">登录后可体验完整功能</text>
					<button @click="toLogin" :disabled="loginDisabled" type="primary"
						class="weixin-login-btn">微信授权登录</button>
				</view>
				<view class="weixin-login-bot">
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/alert-close.png"
						@click="loginShow=false" mode="widthFix" />
				</view>
			</view>
		</u-popup>
		<my-tabbar></my-tabbar>
	</view>
</template>

<script>
	import permision from "@/components/permission.js"
  import {
    getCode,
    isWeiXin
  } from '../../common/helper';
	var _this;
	export default {
		data() {
			return {
				show: false,
				customStyle1: {
					'width': '80%'
				},
				user: {},
				//订单数据
				orderlist: [{
					img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/wait-pay.png",
					text: "待支付"
				}, {
					img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/wait-fh.png",
					text: "待发货"
				}, {
					img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/wait-sh.png",
					text: "待收货"
				}, {
					img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/yiwancheng.png",
					text: "已完成"
				}],
				// 我的资产
				myMoney: [{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/ry-gl.png",
						text: "我的资金",
						page: "pages/center/myMoney"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/ry-gl.png",
						text: "认养管理",
						page: "pages/foster/foster-order-list"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/txzr.png",
						text: "提现转让",
						page: "pages/center/withdrawal"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/jydt.png",
						text: "交易大厅",
						page: "pages/deal/deal-hall"
					}
				],
				// 常用管理
				regularManager: [{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/dizhi.png",
						text: "地址管理",
						page: "pages/center/address-list"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/lishi.png",
						text: "浏览历史",
						page: "pages/center/history"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/collection.png",
						text: "商品收藏",
						page: "pages/center/Collection"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/fxtg.png",
						text: "分销推广",
						page: "pages/center/popularize"
					}
				],
				// 商家服务
				merchantService: [{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/sqsj.png",
						text: "申请商家",
						page: "pages/center/store-apply"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/sqzt.png",
						text: "服务站申请",
						page: "pages/center/zt-apply"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/hxsm.png",
						text: "核销扫码",
						page: "scan"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/jhtd.png",
						text: "进货通道",
						page: "pages/goods/stock-shop"
					}
				],
				// 平台服务
				platFormService: [{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/gonggao.png",
						text: "平台公告",
						page: "pages/index/notice-list"
					},
					{
						img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/lxkf.png",
						text: "联系客服",
						page: "lxkf"
					}
				],
				//常用功能
				// functionlist: [{
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/dizhi.png",
				// 	text: "地址管理",
				// 	page: "pages/center/address-list"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/lishi.png",
				// 	text: "浏览历史",
				// 	page: "pages/center/history"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/collection.png",
				// 	text: "商品收藏",
				// 	page: "pages/center/Collection"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/gonggao.png",
				// 	text: "平台公告",
				// 	page: "pages/index/notice-list"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/fxtg.png",
				// 	text: "分享推广",
				// 	page: "pages/center/qrcode"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/ry-gl.png",
				// 	text: "认养管理",
				// 	page: "pages/foster/foster-order-list"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/tuandui.png",
				// 	text: "团队管理",
				// 	page: "pages/center/team-list"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/jydt.png",
				// 	text: "交易大厅",
				// 	page: "pages/deal/deal-hall"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/txzr.png",
				// 	text: "提现转让",
				// 	page: "pages/center/withdrawal"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/lxkf.png",
				// 	text: "联系客服",
				// 	page: "lxkf"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/sqsj.png",
				// 	text: "申请商家",
				// 	page: "pages/center/store-apply"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/sqzt.png",
				// 	text: "服务站申请",
				// 	page: "pages/center/zt-apply"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/hxsm.png",
				// 	text: "核销扫码",
				// 	page: "scan"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/jhtd.png",
				// 	text: "进货通道",
				// 	page: "pages/goods/stock-shop"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/ydfh.png",
				// 	text: "团队分红",
				// 	page: "pages/center/month-fh"
				// }, {
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/qgfh.png",
				// 	text: "全国分红",
				// 	page: "pages/center/jq-fh"
				// }, ],
				// arr: [{
				// 	img: "https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/safeexit.png",
				// 	text: "安全退出",
				// 	page: ""
				// }, ],
				loginShow: false,
				loginDisabled: false,
			}
		},
		onLoad() {
			_this = this;
		},
		onShow() {
			this.get_user_info()
		},
		onPullDownRefresh() {
			this.get_user_info()
		},
		methods: {
			toLogin() {
				uni.redirectTo({
					url: '/pages/auth/mobile-login'
				})
			},
			toMyMoney() {
				uni.navigateTo({
					url: '/pages/center/myMoney'
				})
			},
			getUserProfile() {
				// #ifdef MP-WEIXIN
				// 获取用户信息
				uni.getUserProfile({
					desc: '用于完善会员资料',
					success: function(infoRes) {
						console.log(infoRes)
						_this.wx_shouquan(infoRes.userInfo)
					},
					fail(fail) {
						console.log(fail)
					}
				});
				// #endif
				// #ifndef MP-WEIXIN
				_this.personalto()
				// #endif
			},
			async wx_shouquan(userInfo) {
				let res = {
					'nickName': userInfo.nickName,
					'avatarUrl': userInfo.avatarUrl
				}
				const data = await _this.$post('api/weixin_shouquan', res)
				if (data.code == 200) {
					_this.user.head_pic = userInfo.avatarUrl
					_this.user.nickname = userInfo.nickName
				}
			},
			//获取用户信息
			async get_user_info() {
				try {
					const data = await _this.$post('api/UserInfo')
					this.user = data.data.user
					this.$post('api/config')
				} catch (e) {
					this.$u.vuex('user_id', '')
					this.$u.vuex('user_token', '')
				} finally {
					setTimeout(() => {
						uni.stopPullDownRefresh()
					}, 500)
				}
			},
			//跳转到订单页
			orderto() {
				if (this.user_id) {
					this.$u.route('pages/order/order-list')
				} else {
					_this.loginShow = true
				}
			},
			//路由跳转
			jump(url, status) {
				if (this.user_id) {
					this.$u.route({
						url,
						params: {
							status
						}
					})
				} else {
					_this.loginShow = true
				}
			},
			jump2(url) {
				this.$u.route({
					url
				})
			},
			//功能跳转
			async functionto(page) {
				if (this.user_id > 0) {
					if (page == 'scan') {
						// #ifdef H5
						this.$u.toast('请在app端使用')
						return
						// #endif
						// #ifdef APP-PLUS
						let status = await this.checkPermission();
						if (status !== 1) {
							return;
						}
						// #endif
						uni.scanCode({
							success: (res) => {
								if (res.result) {
									let arr = res.result.split('_')
									if (arr[0] == 'cityRanch') {
										let order_id = arr[1]
										if (order_id) {
											uni.navigateTo({
												url: `/pages/order/dh-order-hexiao?id=${order_id}`
											})
										}
									} else {
										this.$u.toast('没有扫码到内容')
									}
								} else {
									this.$u.toast('扫码失败')
								}
								console.log(res);
							},
							fail: (err) => {
								// 需要注意的是小程序扫码不需要申请相机权限
								console.log(err);
								this.$u.toast('扫码失败')
							}
						});
					} else if (page == 'lxkf') {
						this.$u.route({
							url: '/pages/index/html-detail',
							params: {
								title: '联系客服',
								url: encodeURIComponent('/api/xieyi_xq?id=4')
							}
						})
					} else if (page == '') {
						this.show = true
					} else {
						this.$u.route({
							url: page
						})
					}
				} else {
					_this.loginShow = true
				}
			},
			async checkPermission(code) {
				let status = permision.isIOS ? await permision.requestIOS('camera') :
					await permision.requestAndroid('android.permission.CAMERA');

				if (status === null || status === 1) {
					status = 1;
				} else {
					uni.showModal({
						content: "需要相机权限",
						confirmText: "设置",
						success: function(res) {
							if (res.confirm) {
								permision.gotoAppSetting();
							}
						}
					})
				}
				return status;
			},
			//跳转到会员中心
			nemberto() {
				if (this.user_id > 0) {
					this.$u.route('pages/center/Teamlist')
				} else {
					_this.loginShow = true
				}
			},
			//跳转到登录
            loginto() {
                // #ifdef H5
                if (isWeiXin()) {
                    uni.$u.route({
                        url: '/pages/auth/mobile-login'
                    })
                } else {
                    uni.$u.route({
                        url: '/pages/auth/login'
                    })
                }
                // #endif

                // #ifndef H5
                uni.$u.route({
                    url: '/pages/auth/mobile-login'
                })
                // #endif
            },
			open() {
				// console.log('open');
			},
			close() {
				this.show = false
				// console.log('close');
			},
			//退出登录
			Logout() {
				this.$u.toast('退出成功')
				setTimeout(() => {
					//清除缓存的token和用户信息
					_this.$u.vuex('user_id', 0)
					_this.$u.vuex('user_token', getApp().globalData.default_token)
					uni.reLaunch({
						url: '/pages/auth/login'
					})
					_this.show = false
					_this.user = {}
				}, 1500)
			},
			//跳转到账户设置
			personalto() {
				this.$u.route('pages/center/account-set')
			},
			//跳转到资金明细
			balanceto(type) {
				this.$u.route({
					url: '/pages/center/capital-list',
					params: {
						type
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .u-navbar__content__title {
		color: #fff !important;
	}

	/deep/ .u-navbar__content__left {
		display: none !important;
	}

	.container {
		height: 100%;
		padding: 0;
	}

	.center_bgi {
		background: url('https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/user/user-bg.png') no-repeat left top/100% 450rpx;
		height: 570rpx;
		position: relative;
		padding: calc(var(--status-bar-height) + 140rpx) 30rpx 0 30rpx;
		box-sizing: border-box;

		.center_head {
			margin: 0 auto;
			display: flex;
			align-items: center;

			.center_head_flex {
				flex: 1;

				.center_head_top {
					color: #fff;
					height: 110rpx;
					font-size: 28rpx;
					@include flex-box(column, center);

					.center_head_flex_1 {
						@include flex-center();
						margin-bottom: 10rpx;

						.nickname {
							font-size: 32rpx;
						}

						.center_head_top_bg {
							border: 2rpx solid #fff;
							color: #fff;
							margin-left: 10rpx;
							padding: 4rpx 16rpx;
							border-radius: 0 15rpx 0 15rpx;
							line-height: 24rpx;
							font-size: 26rpx;
							text-align: center;
						}
					}
				}
			}

			.image_1 {
				width: 130rpx;
				height: 130rpx;
				margin-right: 20rpx;
				border-radius: 50%;
			}

			.right-text {
				color: #fff;
				font-size: 28rpx;
			}

			.image_2 {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}

	.inner {
		position: relative;
		padding: 0 30rpx;
		margin-top: calc((470rpx - var(--status-bar-height) - 210rpx)*-1);

		.center_balance {
			@include flex-center();
			height: 140rpx;
			background: #fff;
			box-shadow: $global-shade;
			border-radius: 20rpx;

			.center_balance_flex {
				flex: 1;
				@include flex-box(column, null, center);
				position: relative;

				&:not(:last-child)::after {
					content: '';
					position: absolute;
					right: 0;
					top: calc(50% - 13rpx);
					width: 2rpx;
					height: 26rpx;
					background: $border-F7F7F7;
				}

				text {
					&:nth-child(1) {
						font-size: 32rpx;
						font-weight: 500;
					}

					&:nth-child(2) {
						font-size: 24rpx;
						margin-top: 5rpx;
					}
				}
			}
		}
	}

	.center_Myorder {
		margin: 20rpx auto 0;
		padding: 20rpx 20rpx;
		box-sizing: border-box;
		box-shadow: $global-shade;
		border-radius: 20rpx;
		background: #fff;

		.center_Myorder_top {
			@include flex-box(row, space-between, center);

			.all-order {
				image {
					width: 10rpx;
				}
			}
		}

		.bor {
			padding: 0 0 20rpx 0;
			border-bottom: 2rpx solid $border-color;
		}

		.center_Myorder_top_text1 {
			font-size: 32rpx;
			font-weight: 500;
		}

		.center_Myorder_top_text2 {
			margin-right: 10rpx;
		}

		.center_Myorder_bottom {
			display: flex;
			margin-top: 10rpx;
			padding: 20rpx 0;
		}

		.center_Myorder_bottom2 {
			display: flex;
			flex-wrap: wrap;
			margin-top: 10rpx;
			padding: 20rpx 0 0 0;
		}

		.center_Myorder_bottom_flex {
			width: 25%;
			display: flex;
			flex-direction: column;
			align-items: center;
		}

		.center_Myorder_bottom_flex2 {
			width: 25%;
			box-sizing: border-box;
			@include flex-box(column, null, center);
			margin: 25rpx 0;
		}
	}

	.center_Myorder_bottom_img {
		width: 48rpx;
		height: 48rpx;
	}

	.center_Myorder_bottom_text {
		margin-top: 20rpx;
	}
</style>